package lu.med.praxiswahl.wahlumed.client.arztbriefe;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

import lu.med.praxiswahl.wahlumed.client.utilities.ContextFactoryRemote;
import lu.med.praxiswahl.wahlumed.client.utilities.ContextFactoryRemoteInt;
import lu.med.praxiswahl.wahlumed.client.utilities.UniCode;
import lu.med.praxiswahl.wahlumed.service.beans.DoctorService;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.DoctorServiceInt;
import lu.med.praxiswahl.wahlumed.client.wclient.Main;

/**
 * 
 * @author Lars Holger Roth
 */
@SuppressWarnings("serial")
public class HausarztChooser extends JFrame implements ActionListener {

	private JRadioButton[] jc;
	private ArztbriefPanel owner;
	private ArztbriefThread thread;

	private Main dataManager;

	public HausarztChooser(Main dataManager, ArztbriefPanel owner,
			ArztbriefThread thread) {
		super("Arzt ausw" + UniCode.getae() + "hlen");
		this.setAlwaysOnTop(true);
		this.owner = owner;
		this.thread = thread;
		this.setLayout(new BorderLayout());
		this.dataManager = dataManager;
		// Das Fenster besteht einfach aus einer Reihe von Check Boxes mit
		// den Eintr�gen
		JPanel p = new JPanel(new GridLayout(0, 1));
		jc = new JRadioButton[dataManager.getArztDatabase().getSize()];
		ButtonGroup bg = new ButtonGroup();
		for (int i = 0; i < jc.length; i++) {
			jc[i] = new JRadioButton(dataManager.getArztDatabase()
					.getFullEntryText(i));
			bg.add(jc[i]);
			p.add(jc[i]);
		}

		JScrollPane sp = new JScrollPane(p);

		this.add(sp, BorderLayout.CENTER);

		JPanel bPanel = new JPanel();
		JButton a, b;
		a = new JButton("Ok");
		a.addActionListener(this);
		b = new JButton("Abbrechen");
		b.addActionListener(this);
		bPanel.add(a);
		bPanel.add(b);
		this.add(bPanel, BorderLayout.SOUTH);

		this.pack();
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("Ok")) {

			// Welcher Eintrag wurde makiert
			int index = -1;
			for (int i = 0; i < jc.length; i++) {
				if (jc[i].isSelected()) {
					index = i;
					break;
				}
			}

			DoctorServiceInt doc = dataManager.getDoctorContext();

			// Liste mit IDs abrufen
			Long[] tmpDoctors = doc.hausAerzteAnzeigen();
			/*
			 * str +=
			 * dataManager.getArztDatabase().getDataset(index).getBufferAt(0) +
			 * "_" +
			 * dataManager.getArztDatabase().getDataset(index).getBufferAt(1);
			 */
			owner.setMostImportantDoctorID(tmpDoctors[index]);
			thread.setExitThis();
			this.setVisible(false);
		} else {
			thread.setExitThis();
			this.setVisible(false);
		}
	}

	@Override
	public void setVisible(boolean in) {
		super.setVisible(in);
		this.setExtendedState(Frame.MAXIMIZED_BOTH);
	}

}
